UIMainPage = UIBase.New(PanelNames.UIMainPage)

function UIMainPage:OnCreate()
    self.btnHero = self:GetChild("Btn_Hero")
    self.btnGoInBattle = self:GetChild("Btn_Drama")
    self.btnGoExplore = self:GetChild("Btn_Inference")
    self:AddEvent()
    self:InitView()
end

function UIMainPage:AddEvent()
    self:RegisterOnClick(self.btnHero,self.OnClickHero)
    self:RegisterOnClick(self.btnGoInBattle,self.OnClickBattle)
    self:RegisterOnClick(self.btnGoExplore,self.OnClickExplore)
end

function UIMainPage:InitView()
    self:Refresh()
end

function UIMainPage:Refresh()
end

function UIMainPage:OnClickBattle()
    local curUseDeckIdx = decodeJsonStrToTable(GetPlayerPrefs("SelectedDeckIndex")) or 1
    if curUseDeckIdx and curUseDeckIdx > 0 then
        local heroList = decodeJsonStrToTable(GetPlayerPrefs("SelectedHeroList"..curUseDeckIdx))
        if heroList and #heroList >0 then
            closeUI(PanelNames.UIMainPage)
            local cardList = {}
            for k,v in pairs(heroList) do
                table.insert(cardList,Battle.Card.New():Set(v,1))
            end
            local bat = Battle.CBattle.CreateBattle()
            bat:SetCardList(bat:CreateCardsByIdList(heroList),bat:CreateCardsByIdList(decodeJsonStrToTable(Battle.GlobalConfig.GetString(21))))
            bat:LoadAndFight()
        else
            logError("请先在布阵界面选择上阵英雄")
            return
        end
    else
        logError("请先在布阵界面选择上阵卡组")
        return
    end
end

function UIMainPage:OnClickExplore()
    Battle.SceneMgr:OnCreate()
    closeUI(PanelNames.UIMainPage)
    --showUI(PanelNames.UIJoyMain)
    Battle.SceneMgr.LoadScene("Scenes/explore/explore",function()
    end,nil)
end

function UIMainPage:OnClickHero()
    showUI(PanelNames.UICardDeck)
end

return UIMainPage